package com.RBGame;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

import android.widget.Button;
import android.widget.TextView;

/**
 * Game: a simple game that everyone can enjoy.
 * 
 * This is an implementation of the classic Game "Game", in which you control a
 * serpent roaming around the garden looking for apples. Be careful, though,
 * because when you catch one, not only will you become longer, but you'll move
 * faster. Running into yourself or the walls will end the game.
 * 
 */

public class RBGameActivity extends Activity {

    private GameView mGameView;
    
    private static String ICICLE_KEY = "Game-view";
    
    Button m_undo_button ;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game_layout);
        mGameView = (GameView) findViewById(R.id.game);
        mGameView.setTextView((TextView) findViewById(R.id.text));
        m_undo_button = (Button) findViewById(R.id.undo_button);
        mGameView.setUndoButton(m_undo_button);
        System.out.println("here start Activity");
        FileOutputStream fout = null;
        FileInputStream  fin = null;
        try {
			fin = openFileInput("back.txt");
		} catch (FileNotFoundException e) {
			try {
				/*
				 *  New User data file
				 */
				fout = openFileOutput("back.txt",Context.MODE_PRIVATE);
				OutputStreamWriter foutW = new OutputStreamWriter (fout);
				foutW.write("12");
				foutW.close();
				fout.close();
			} catch (FileNotFoundException e1) {e1.printStackTrace();}
			catch (IOException e1) {e1.printStackTrace();}
			
			
			try {
				fin = openFileInput("back.txt");
			} catch (FileNotFoundException e1) {e1.printStackTrace();}
			
		}
        int k;
        String Rline = "";
        BufferedReader finBufR = new BufferedReader(new InputStreamReader(fin));
        try {
			Rline = finBufR.readLine();
	        System.out.println("hi"+Rline+"hi");
	        fin.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
        try {
			fout = openFileOutput("back.txt",Context.MODE_PRIVATE);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		OutputStreamWriter foutW = new OutputStreamWriter (fout);
		try {
			
			foutW.write(String.valueOf(Integer.parseInt(Rline)+1));
			foutW.close();
			fout.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        m_undo_button.setOnClickListener(mGameView.myButtonListener);
        ((Button) findViewById(R.id.newgamebutton)).setOnClickListener(mGameView.myButtonListener);
        ((Button) findViewById(R.id.prev_button)).setOnClickListener(mGameView.myButtonListener);
        ((Button) findViewById(R.id.next_button)).setOnClickListener(mGameView.myButtonListener);
        ((Button) findViewById(R.id.next_ten)).setOnClickListener(mGameView.myButtonListener);
        ((Button) findViewById(R.id.prev_ten)).setOnClickListener(mGameView.myButtonListener);
        
    	mGameView.setNewGameButton((Button) findViewById(R.id.newgamebutton));
    	mGameView.setmNextButton((Button) findViewById(R.id.next_button));
    	mGameView.setmPrevButton((Button) findViewById(R.id.prev_button));
    	//mGameView.setmRedoButton((Button) findViewById(R.id.redo_button));
    	mGameView.setmMatchLeftText((TextView) findViewById(R.id.matchLeftText));
    	mGameView.setMlevelNameText((TextView) findViewById(R.id.levelnameText));
    	mGameView.setmNext10Button((Button) findViewById(R.id.next_ten));
    	mGameView.setmPrev10Button((Button) findViewById(R.id.prev_ten));
        if (savedInstanceState == null) {
            // We were just launched -- set up a new game
            // mGameView.setMode(GameView.READY);
        	mGameView.initNewGame();
        } else {
            // We are being restored
            Bundle map = savedInstanceState.getBundle(ICICLE_KEY);
            if (map != null) {
                mGameView.restoreState(map);
            } else {
            	mGameView.initNewGame();
               // mGameView.setMode(GameView.PAUSE);
            }
        }
    }
    
    protected void changeUndoTitle() {
       m_undo_button.setText(m_undo_button.getText()+"!");
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        // Pause the game along with the activity
        //mGameView.setMode(GameView.PAUSE);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        //Store the game state
        outState.putBundle(ICICLE_KEY, mGameView.saveState());
    }

}
